/******------ 用户信息帮助类---------********/
/******------ 此类为辅助类为了转换服务器返回的数据---------********/

/**
 * 二级菜单转换类
 * @param  {[type]} params 原二级菜单数据
 * @return {[type]}        转换后二级菜单数据
 */
export function getSiderMenus(params) {
  if (!params?.length) return [];
  return params.map((n) => {
    let obj = {};
    let chid = getSiderMenus(n.children);
    if (chid.length > 0) {
      obj.children = chid;
    }
    return { ...n.root, ...obj };
  });
}

/**
 * 获取二级菜单地一个显示
 * @param  {[type]} array   [description]
 * @param  {[type]} menuKey [description]
 * @return {[type]}         [description]
 */
export function getMeunFistKey(array, menuKey) {
  if (!array.length) {
    return { dsplayMenuKey: '', breadPath: '' };
  }
  if (menuKey) {
    return {
      dsplayMenuKey: menuKey,
      menuCode: getMeunCode(array, menuKey),
      breadPath: getMeunKey(array, menuKey),
    };
  }
  let keyVal = '';
  let fistCode = '';
  let breadPath = '';
  let fistItem = array[0];
  if (!fistItem) {
    return keyVal;
  }
  if (fistItem.children && fistItem.children.length > 0) {
    let obj = getMeunFistKey(fistItem.children);
    if (breadPath.length > 0) {
      breadPath += '/';
    }
    keyVal = obj.dsplayMenuKey;
    fistCode = obj.menuCode;
    breadPath += obj.breadPath;
  }
  if (breadPath.length > 0) {
    breadPath += '/';
  }
  breadPath += fistItem.menuName;

  if (keyVal === '') {
    fistCode = fistItem.menuCode;
    keyVal = fistItem.component;
  }
  breadPath = breadPath.split('/').reverse().join('/');
  return { dsplayMenuKey: keyVal, menuCode: fistCode, breadPath: breadPath };
}

// t_data_source 数据源 ,t_station 节算单元 ,t_region 区域, t_decompose_strategy 策略
export const powersDataType = {
  settlement: 'sell_electricity_company',
  source: 't_data_source',
  station: 't_station',
  region: 't_region',
  dept: 't_dept',
  strategy: 't_decompose_strategy',
  province_region: 't_province_region',
};

/**
 * 获取菜单的code值
 * @param  {[type]} array  [description]
 * @param  {[type]} keyVal [description]
 * @return {[type]}        [description]
 */

export function getMeunCode(array, keyVal) {
  let codeObj = { codeStr: '' };
  array.forEach((item) => {
    if (item.component == keyVal) {
      codeObj.codeStr = item.menuCode;
    } else {
      if (item.children) {
        findMenucode(item.children, keyVal, codeObj);
      }
    }
  });
  return codeObj.codeStr;
}

function findMenucode(array, keyVal, codeObj) {
  array.forEach((item) => {
    if (item.component == keyVal) {
      codeObj.codeStr = item.menuCode;
    } else {
      if (item.children) {
        findMenucode(item.children, keyVal, codeObj);
      }
    }
  });
}
/**
 * 获取菜单的key值
 * @param  {[type]} array  [description]
 * @param  {[type]} keyVal [description]
 * @return {[type]}        [description]
 */
export function getMeunKey(array, keyVal) {
  let arr = keyVal.split('/');
  let breadPath = '';
  let temp = array;
  let tempKey = '';
  arr.forEach((item) => {
    tempKey += item;
    let x = getOneMeunKey(temp, tempKey);
    if (x !== undefined) {
      if (breadPath.length > 0) {
        breadPath += '/';
      }
      if (tempKey.length > 0) {
        tempKey += '/';
      }
      breadPath += x.breadName;
      if (x.array !== null) {
        temp = x.array;
      }
    }
  });
  return breadPath;
}

const getOneMeunKey = function (array, keyVal) {
  let arr = array.filter((item) => item.component === keyVal);
  if (arr.length > 0) {
    const item = arr[0];
    return { array: item.children, breadName: item.menuName };
  }
  return undefined;
};

/**
 * 将路由设置为平铺
 * @param  {[type]} data  [description]
 * @param  {[type]} treeMap [description]
 * @param  {[type]} depth [description]
 * @return {[type]}        [description]
 */
export const tiledMenuToArray = (data, treeMap = [], depth = 0) => {
  let lintDepth = depth;
  if (!(data && data.length)) return [];
  lintDepth++;
  return data.reduce((acc, cur) => {
    cur.depth = lintDepth;
    acc.push(cur);
    if (cur.children && cur.children.length) {
      tiledMenuToArray(cur.children, treeMap, lintDepth);
    }
    return acc;
  }, treeMap);
};

export const logoWEB = 'logoWEB';

const getDefaultApp = (array, menuKey) => {
  //没有访问记录，则根据路由查找
  const m = array.find((m) => m.menuCode == menuKey.menu);
  let obj = { findLogo: false };
  if (m) {
    obj.findLogo = m.url == logoWEB;
    obj = { ...m, ...obj };
  } else {
    let firstApp = {};
    array.some((m) => {
      if (m.hasOwnProperty('applicationExplain')) {
        firstApp = m;
        return true;
      }
      return false;
    });
    return { ...firstApp, ...obj };
  }
  return obj;
};

/**
 * 找跳转的路由
 * @param  {[type]} array  [description]
 * @return {[type]}        [description]
 */
export const findReplace = (array = [], menuKey, payload, visit = {}, appName = '') => {
  let obj = { findLogo: false };
  if (!visit.customApplicationId) return getDefaultApp(array, menuKey);
  const findLogo = array.find((item) => item.url === logoWEB);
  if (findLogo) obj.findLogo = true; // 寻找是否可以修改logo
  // 通过参数判断是否为EIS外链EAM
  if (
    menuKey.menu === 'eamManagement' &&
    menuKey.menuParam.indexOf('form_key') > -1 &&
    menuKey.menuParam.indexOf('id') > -1
  ) {
    const menuKeyObj = array.find((item) => item.path === '/' + menuKey.menu);
    if (menuKeyObj) return { ...obj, ...menuKeyObj };
  }
  // 是否要跳入指定的路由
  if (payload?.url) {
    const menuKeyObj = array.find((item) => item.url === payload.url);
    if (menuKeyObj) return { ...obj, ...menuKeyObj };
  }
  // firstJump 登录或者刷新时判断、手动输入地址
  // menuKey 获取当前的路由地址
  if (menuKey?.menu && payload?.firstJump) {
    const menuKeyObj = array.find(
      (item) =>
        item.url === menuKey.menu &&
        (visit.customApplicationId == item.customApplicationId ||
          appName == item.customApplicationName),
    );
    if (menuKeyObj) return { ...obj, ...menuKeyObj, url: menuKey.menuParam };
  }
  //登录跳转过来的,路径是/
  if (!menuKey?.menu && payload?.firstJump) {
    const menuKeyObj = array.find(
      (item) =>
        item.menuId == visit.secondMenuId && visit.customApplicationId == item.customApplicationId,
    );
    if (menuKeyObj) return { ...obj, ...menuKeyObj, url: menuKey.menuParam };
  }
  const replaceItem = array.find(
    (item) =>
      item.homePageFlag &&
      item.url &&
      item.url !== logoWEB &&
      visit.customApplicationId == item.customApplicationId,
  );
  if (replaceItem?.url) {
    return { ...obj, ...replaceItem };
  } else {
    const replaceUrl = array.find(
      (item) =>
        item.url && item.url !== logoWEB && visit.customApplicationId == item.customApplicationId,
    );
    if (replaceUrl?.url) return { ...obj, ...replaceUrl };
    else return obj;
  }
};

/**
 * 获取菜单的 menuCode
 *  * @param  {[type]} array  [description]
 * @param  {[type]} keyVal [description]
 * @return {[type]}        [description]
 */
export const findMeunCode = (menuArray, url) => {
  return menuArray.find((item) => item.url === url)?.menuCode || '';
};
